<?php

function sumbotaccountupdatefile()
{
    $searchdir = "E:\\qqxj_update\\file\\update";
    $aDirectory = new DirReader($searchdir);
    
    $fileListArray = $aDirectory->getFileList();
    
    
    //account=1812985488&rolename=%CB%C6%BA%F5%D6%D8%D0%C2&state=on_attack&mapid=ch_bldg_slum1&money=152283&itemcount=0&position=727.705933,80.331032,94.920959&killmoncount=0&skillbuf=&level=22&fatigue=1000&job=JobClassWarrior&exp=272687&selectchannel=7
    foreach($fileListArray as $filename)
    {
        if(strstr($filename,'.update') == true)
        {
            $filehandle = fopen($searchdir.'/'.$filename,'r');
            
            $strline = fgets($filehandle,'1024');
           
           	$matches = explode('&',$strline);
            $datas = array();
            foreach($matches as $match)
            {
                $kvs = explode('=',$match);
                $datas[$kvs[0]] = $kvs[1];
            }

            $sql_daily_weblogs = "CREATE TABLE IF NOT EXISTS `$after_table_name` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `ip` char(16) NOT NULL,
    `date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
    `gatetag` varchar(32) NOT NULL,
    `gateurl` varchar(256) NOT NULL,
    `installed` int(11) NOT NULL DEFAULT 0, 
    `browseinfo` varchar(256) NOT NULL,
    `md5` varchar(32) NOT NULL,
    PRIMARY KEY (`id`),
    KEY (`ip`),
    KEY (`gatetag`),
    KEY `date` (`date`),
    KEY `installed` (`installed`),
    KEY `md5` (`md5`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;";


    //$conn = mysql_connect('208.88.18.27','webuser','sunnytown123456');
    $conn = mysql_connect('localhost','qqxj','qqxj');
    mysql_select_db('analysis_st',$conn);
    
    $droptable = "truncate table {$after_table_name}";
    
    $rs = mysql_query($droptable,$conn);
    if (!$rs) 
    {
       echo "{$after_table_name} truncate table".mysql_error();
    } 
    else 
    {
        echo "{$after_table_name} drop Table  success.\n";
    }
     $rs = mysql_query($sql_daily_weblogs,$conn);
    if (!$rs) 
    {
        exit('Daily Table create failed'.mysql_error());
    } 
    else 
    {
        echo "{$after_table_name} Table create success.\n";
    }
    
    return $conn;
    
            
        }
         
        
    }
    
}

// 解压web log 文件
function parselog($daydata)
{
    $day = str_replace("-","",$daydata);
    // -- get data from full web logs
   
    if(PATH_SEPARATOR==':') 
    {
        $logdir = "/data/test/{$day}";
    }
    else 
    {
        $logdir = 'E:\\20110525';
    }
    if(!is_dir($logdir))
    {
        mkdir($logdir,0,true);
    }
    else
    {
        echo 'dir exist'."\n";
        exec("rm -r -f {$logdir}");
        mkdir($logdir,0,true);
    }
        
    exec("chmod 755 {$logdir}");
    
    //$log_file    = "E:\\st.ucool.com.access.log.20110801\\st.ucool.com.access.log.20110801";
    //$log_file      = "/data/test/access_20110627.log";
    if(PATH_SEPARATOR==':') 
    {
        $log_file    = "/data/test/access_{$day}.log";
        if(!file_exists($log_file))
        {
            $fileh = fopen($log_file,'a');
            fclose($fileh);
        }
        $sh = "zcat /data/weblogs/st.ucool.com.access.log.{$day}.gz > {$log_file}";
        exec($sh);

    }
    else
    {
        
        $log_file = "E:\\st.ucool.com.access.log.20110525\\st.ucool.com.access.log_20110525235318";
    }
    
    // -- parse need logs
    $eregid_str = '|([0-9\.]*) ([a-zA-Z0-9\.]*) - \[(.*)\] "[get\|post]* ([^"]*) HTTP/([0-9\.]*)" ([0-9]*) ([0-9]*) "([^"]*)" "([^"]*)" "([^"]*)"|i';

    $aff_data = array();
    $i = 0;

    $ff = fopen($log_file,'r');
    if($ff == null)
    {
        exit($log_file. ' is Not File');
    }
    
    $run_time = 0;
    $ipmappostorget = array();
    $str = '';
    while (!feof($ff)) 
    {
        $str1111 = fgets($ff,'1024');
        
        /* if (strstr($str1111,'31/May/2011') == false && strstr($str1111,'01/Jun/2011') == false)
        {
            continue;
        } */
            
        
        preg_match_all($eregid_str, $str1111, $matches, PREG_SET_ORDER);

        foreach ($matches as $key => $val) 
        {
            if (11 == count($val)) 
            {
                 $ip = $val[1];
                
                $iptofile = $logdir."/".$ip.".log";
                
                /*if (file_exists($iptofile))
                    {
                        $findsame = false;
                        $iplogfile = fopen($iptofile,"r");
                        
                        
                        while (!feof($iplogfile)) 
                        {
                            $tmpstrline = fgets($iplogfile,'1024');
                            
                            if(stristr($tmpstrline,$val[3]) == true
                                && stristr($tmpstrline,$val[1]) == true
                                && stristr($tmpstrline,$val[8]) == true
                                && stristr($tmpstrline,$val[9]) == true
                                && stristr($tmpstrline,$val[4]) == true)
                                {
                                    $findsame = true;
                                }
                            
                             preg_match_all($eregid_str, $tmpstrline, $temmatches, PREG_SET_ORDER);
                             
                            foreach ($temmatches as $keyttt => $valttt) 
                            {
                                if (11 == count($valttt))
                                {
                                    if (stristr($val[3],$valttt[3]) == true 
                                        && stristr($val[1],$valttt[1]) == true
                                        && stristr($val[8],$valttt[8]) == true
                                        && stristr($val[9],$valttt[9]) == true
                                        && stristr($val[4],$valttt[4]) == true
                                        )
                                    {
                                        //echo $valttt[3]." writed {$valttt[1]}.log file \n";
                                        $findsame = true;
                                    }
                                }
                            } 
                            if ($findsame)
                                break;
                            
                            
                        }
                        
                        fclose($iplogfile);
                        
                        if ($findsame == false)
                        {
                            $iplogfile = fopen($iptofile,"a");
                            $logdata = $val[0]."\n";
                            fwrite($iplogfile,$logdata,strlen($logdata));
                            fclose($iplogfile);
                        }
                        
                        
                    }
                    else
                    */
                    {
                        $iplogfile = fopen($iptofile,"a");
                        $logdata = $val[0]."\n";
                        fwrite($iplogfile,$logdata,strlen($logdata));
                        fclose($iplogfile);
                    }
                    
               

                
                
            }
        }
        unset($matches);
        unset($str1111);
    }
    
    
}


// 获取一个文件夹下面的所有文件,不包括子文件夹
class DirReader
{   
  private $dh;   
  private $basedir;   
  private $fileNameArray=array();   
  private $dirNameArray=array();   
  
  function __construct($dirname)
  {   
    $this->basedir = $dirname;   
    $this->dh = dir($dirname) or die($php_errormsg);   
    $this->parseDirectory();   
  }   
  
  function parseDirectory()
  {   
    $filename = "";   
    while (false !== ($filename = $this->dh->read())){   
      $fullpath = $this->basedir . '/' . $filename;   
      if (is_file($fullpath)){   
        array_push($this->fileNameArray, $filename);   
      }else{   
        array_push($this->dirNameArray, $filename);   
      }   
    }   
  }   
  
  
  // Get all the files in the directory   
  function getFileList()
  {   
    return $this->fileNameArray;   
  }   
  
  // Get all the sub directories in the directory   
  function getDirList()
  {   
    return $this->dirNameArray;   
  }   
  
  function getDirPath()
  {   
    return $this->dh->path;   
  }   
} 

// 分析web log 请求入口 放入数据库
function parseiplogfile($curday)
{
    $day = str_replace("-","",$curday);
    if(PATH_SEPARATOR==':') 
        $dirpath = "/data/test/{$day}";
    else 
        $dirpath = 'E:\\20110731';
    
    $aDirectory = new DirReader($dirpath);
    $fileListArray = $aDirectory->getFileList();
    
    $eregid_str = '|([0-9\.]*) ([a-zA-Z0-9\.]*) - \[(.*)\] "[get\|post]* ([^"]*) HTTP/([0-9\.]*)" ([0-9]*) ([0-9]*) "([^"]*)" "([^"]*)" "([^"]*)"|i';
    $allowcount = 0;
    $allowacceptcount = 0;
    $FbDeauthorizecount = 0;
    $postsizearray = array();
    $predaytime = strtotime('2011-07-27 23:59:59');
    $ipacceptallcount = 0;
    $bookmarkcount = 0;
    
    $installedcount = 0;
    $allowacceptlogfile = array();
    $postnoparamcount = 0;
    $postnoparamcountfile = array();
    //$acceptrequesthandle = fopen("E:\\20110731_acceptrequest.txt",'a');
    $after_table_name = "fbpostgate_{$day}";
    if(PATH_SEPARATOR==':')
        $sqlcon = createdbtable($after_table_name);
    else
        $postgate = fopen("E:\\20110731_postgate.txt",'a');

    
    $sql = "insert into {$after_table_name} (ip,date,gatetag,gateurl,installed,browseinfo,md5) values ";

    foreach($fileListArray as $filename)
    {
        
        
        if(PATH_SEPARATOR==':')
            $filehandle = fopen($dirpath.'/'.$filename,'r');
        else
            $filehandle = fopen($dirpath.'\\'.$filename,'r');
        
        $matches = null;
        $fileallowcount = 0;
        $filenoallowcount = 0;
        $jumpallow = false;
        $jumpallowaccept = false;
        $prestrline = '';
        $findpostgate = false;
        $date = '';
        $ip = '';
        $gatetag = '';
        $browseinfo = '';
        $gateurl = '';
        $installed = 0;
        $findpostgatecount = 0;
        $value = '';
        while (!feof($filehandle)) 
        {
            $strline = fgets($filehandle,'1024');
            
            if (strstr($strline,'POST') != false && stristr($strline,'apps.facebook.com/sunnytown') != false)
            {

                preg_match_all($eregid_str, $strline, $matches, PREG_SET_ORDER);

                foreach ($matches as $key => $val) 
                {
                     if(stristr($val[4],'feed%2freward') == false 
                    && stristr($val[4],'feed/freward') == false 
                    && stristr($val[4],'gift%2freceive') == false
                    && stristr($val[4],'gift/receive') == false
                    && stristr($val[4],'gift%2freceivefromneigbhor') == false
                    && stristr($val[4],'gift/freceivefromneigbhor') == false
                    && stristr($val[4],'gift%2freceiveitem') == false
                    && stristr($val[4],'gift/receiveitem') == false
                    && stristr($val[4],'neighbor%2faccept') == false
                    && stristr($val[4],'neighbor/accept') == false
                    && stristr($val[4],'request%2facceptbuildinghelp') == false
                    && stristr($val[4],'request/acceptbuildinghelp') == false
                    && stristr($val[4],'request%2facceptrequiregift') == false
                    && stristr($val[4],'request/acceptrequiregift') == false
                    && stristr($val[4],'train%2ftrainaccept') == false
                    && stristr($val[4],'train/trainaccept') == false
                    && stristr($val[4],'bookmarks') == false
                    && stristr($val[4],'notif') == false
                    && stristr($val[4],'canvas_bkmk_more') == false
                    && stristr($val[4],'canvas_bkmk_top') == false
                    && stristr($val[4],'games_friends_apps') == false
                    && stristr($val[4],'games_my_recent') == false
                    && stristr($val[4],'discovery') == false
                    && stristr($val[4],'friends') == false
                    && stristr($val[4],'ts') == false
                    && stristr($val[4],'/?count') == false
                    && stristr($val[4],'appd_my_recent') == false
                    && ($val[4] =="/") == false
                    && stristr($val[4],'Farm%2FAcceptCropHelp') == false
                    && stristr($val[4],'Business%2FAcceptHelp') == false
                    )
                    {
                        break;
                    }
                   
                    $date = date("Y-m-d H:i:s",strtotime($val[3]));
                    $ip = $val[1];
                    $gatetag = $val[4];
                    $browseinfo = $val[9];
                    $gateurl = $val[8];
                    $findpostgate = true;
                    $md5data = md5($ip.$browseinfo); 
                    $browseinfo = str_replace("'","",$browseinfo);                    
                    $value = $value."('{$ip}','{$date}','{$gatetag}','{$gateurl}','{$installed}','{$browseinfo}','{$md5data}'),";
                    
                    
                }
                
                
                    
            }
           
            

            
            
        }
        //ip,date,gatetag,gateurl,installed,browseinfo
        
        if(PATH_SEPARATOR==':')
        {
            if($findpostgate)
            {
            	$value[strlen($value) - 1] = ';';
                $insertsql = $sql . $value;
                
                $rs = mysql_query($insertsql,$sqlcon);
                if (!$rs) 
                {
                    echo $insertsql."\n";
                    exit('allow update failed'.mysql_error()."\n");
                } 
                else 
                {
                    //echo date("Y-m-d H:i:s")."Allow update success. \n";
                }
            }
            
        }
        else
        {
            fwrite($postgate,$value,strlen($value));
            fclose($postgate);
        }
        
    }
   
}
// 
function parseonefile($filepath,$longtime)
{
        $filehandle = fopen($filepath,'r');
        
        if ($filehandle == null)
        	return;
        
        $matches = null;
        $fileallowcount = 0;
        $filenoallowcount = 0;
        $jumpallow = false;
        $jumpallowaccept = false;
        $prestrline = '';
        $eregid_str = '|([0-9\.]*) ([a-zA-Z0-9\.]*) - \[(.*)\] "[get\|post]* ([^"]*) HTTP/([0-9\.]*)" ([0-9]*) ([0-9]*) "([^"]*)" "([^"]*)" "([^"]*)"|i';
        $allowacceptcount = 0;
        $allowacceptlogfile = array();
        //$giftrequestfilehande = fopen("/data/test/giftrequestfile_5min.txt","a");
        $findgift = false;
        $writefindgiftcount = 3;
        $strlogdata = array();
        $index = 0;
        while (!feof($filehandle)) 
        {
            $strline = fgets($filehandle,'1024');
            
            if (strstr($strline,'POST') != false )
            {
                preg_match_all($eregid_str, $strline, $matches, PREG_SET_ORDER);

                foreach ($matches as $key => $val) 
                {
               
                    echo $val[0];
                    /*                     
                    if (abs($longtime - strtotime($val[3])) <= 60 * 5 && strstr($strline,'Gift%2FReceive') == true)
                    {
                        if (!isset($strlogdata[$index]))
                            $strlogdata[$index] = array();
                        $strlogdata[$index][0] = $strline;
                        $tim1 = strtotime($val[3]);
                        $strlogdata[$index][1] = abs($longtime - $tim1);
                        $strlogdata[$index][2] = $tim1;
                        $index = $index + 1;
                        /* fwrite($giftrequestfilehande,$filepath,strlen($filepath));
                        fwrite($giftrequestfilehande,"\n",strlen("\n"));
                        fwrite($giftrequestfilehande,$strline,strlen($strline)); 
                        
                    } 
                    */
                   /*  elseif(strstr($strline,'Gift%2FReceive') == true)
                    {
                        fwrite($giftrequestfilehande,"over 5 mini\n",strlen("over 5 mini\n"));
                        
                    }
                    */
                    /* 
                   if($val[7] > 300 && $val[7] < 1000 
                   && strstr($strline,'Accepted') == false 
                   && strstr($strline,'Gift/SendToNeigbhor') == false
                   && strstr($strline,'Gift/SendAll') == false
                   && strstr($strline,'Other/Skip') == false
                   && strstr($strline,'Request/SendRequireGif') == false
                   && strstr($strline,'Request/SendBuildingHelp') == false
                   && strstr($strline,'gift/received') == false
                   
                   
                   )
                   {
                       $jumpallow = true;
                       $prestrline = $strline;
                       
                   }
                   if ($val[7] > 3000)
                   {
                       if($jumpallow)
                       {
                           
                           $allowacceptcount = $allowacceptcount + 1;
                           $jumpallow = false;
                       }
                       else
                       {
                           $jumpallow = false;
                       }
	                   	
	               } */
                	
                }
                    
            }
            /* elseif($findgift && $writefindgiftcount > 0)
            {
                $writefindgiftcount = $writefindgiftcount - 1;
                fwrite($giftrequestfilehande,$strline,strlen($strline));
                
                if($writefindgiftcount == 0)
                    $findgift = false;
                 
                
            } */

            
            
            
        }
        if(count($strlogdata) > 0)
        {
            $writestrl = '';
            $lesstime = 1000 * 60 * 60 * 24;
            $mintime = 0;
            $logtimeinfile = 0;
            foreach($strlogdata as $val)
            {
                if($val[1] < $lesstime)
                {
                    $lesstime = $val[1];
                    $writestrl = $val[0];
                    $mintime = $val[1];
                    $logtimeinfile = $val[2];
                }
            }
            
            fwrite($giftrequestfilehande,$filepath,strlen($filepath));
            fwrite($giftrequestfilehande,"\n",strlen("\n"));
            $timestr = 'dbtime:'.$longtime.','.'logtime:'.$logtimeinfile.','.'abs time:'.$mintime;
            fwrite($giftrequestfilehande,$timestr,strlen($timestr));
            
            fwrite($giftrequestfilehande,"\n",strlen("\n"));
            fwrite($giftrequestfilehande,$writestrl,strlen($writestrl));
            
        }
        
        fclose($giftrequestfilehande);
        fclose($filehandle);
}
function parselogip()
{

}
// 解压分析 627 log
function parselog627log()
{
     $day = "20110627";

    $log_file    = "/data/test/access_{$day}.log";
   
    // -- parse need logs
    $eregid_str = '|([0-9\.]*) ([a-zA-Z0-9\.]*) - \[(.*)\] "[get\|post]* ([^"]*) HTTP/([0-9\.]*)" ([0-9]*) ([0-9]*) "([^"]*)" "([^"]*)" "([^"]*)"|i';

    $aff_data = array();
    $i = 0;

    $ff = fopen($log_file,'r');
    if($ff == null)
    {
        exit($log_file. ' is Not File');
    }
    
    $run_time = 0;
    $ipmappostorget = array();
    $str = '';
    
    while (!feof($ff)) 
    {
        $strline = fgets($ff,'1024');
        
        preg_match_all($eregid_str, $strline ,$matches, PREG_SET_ORDER);

        foreach ($matches as $key => $val) 
        {
            if (11 == count($val)) 
            {

                $dateinfo = getdate(strtotime($val[3]));
                
                if ($dateinfo["hours"] < 2)
                {
                    
                    
                        $dayttt = date("Ymd",strtotime($val[3]) - 3600 * 2);
                        
                        $daydir = "/data/test/".$dayttt;
                        if (!is_dir($daydir))
                            mkdir($daydir);
                        
                        $ip = $val[1];
                        $logdir = $daydir;
                        $iptofile = $logdir."/".$ip.".log";
                        
                        if(file_exists($iptofile))
                        {
                            $findsame = false;
                            $iplogfile = fopen($iptofile,"r");
                            
                            while (!feof($iplogfile)) 
                            {
                                $tmpstrline = fgets($iplogfile,'1024');
                                
                                preg_match_all($eregid_str, $tmpstrline, $temmatches, PREG_SET_ORDER);
                                 
                                foreach ($temmatches as $keyttt => $valttt) 
                                {
                                    if (11 == count($valttt))
                                    {
                                        if (stristr($val[3],$valttt[3]) == true 
                                            && stristr($val[1],$valttt[1]) == true
                                            && stristr($val[8],$valttt[8]) == true
                                            && stristr($val[9],$valttt[9]) == true
                                            && stristr($val[4],$valttt[4]) == true
                                            )
                                        {
                                            echo $valttt[3]." writed {$valttt[1]}.log file \n";
                                            $findsame = true;
                                        }
                                    }
                                }
                                if ($findsame)
                                    break;
                                
                                
                            }
                        
                            fclose($iplogfile);
                            
                            if ($findsame == false)
                            {
                                $iplogfile = fopen($iptofile,"a");
                                fwrite($iplogfile,$strline,strlen($strline));
                                fclose($iplogfile);
                            }
                        }
                        else
                        {
                            $iplogfile = fopen($iptofile,"a");
                            fwrite($iplogfile,$strline,strlen($strline));
                            fclose($iplogfile);
                        }
                        
                    
                   
                    
                    
                }
                else
                {
                    $dayttt = date("Ymd",strtotime($val[3]));

                    $daydir = "/data/test/".$dayttt;
                    if (!is_dir($daydir))
                        mkdir($daydir);
                    
                    
                    $ip = $val[1];
                    $logdir = $daydir;
                    $iptofile = $logdir."/".$ip.".log";
                    
                    $iplogfile = fopen($iptofile,"a");
                    fwrite($iplogfile,$strline,strlen($strline));
                    fclose($iplogfile);
                }
               
                
                
            }
        }
        unset($matches);
        unset($strline);
    }
    
}


//创建请求入口数据库
function createdbtable($after_table_name)
{
    //$after_table_name = 'fbpostgate_20110531';
    $sql_daily_weblogs = "CREATE TABLE IF NOT EXISTS `$after_table_name` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `ip` char(16) NOT NULL,
    `date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
    `gatetag` varchar(32) NOT NULL,
    `gateurl` varchar(256) NOT NULL,
    `installed` int(11) NOT NULL DEFAULT 0, 
    `browseinfo` varchar(256) NOT NULL,
    `md5` varchar(32) NOT NULL,
    PRIMARY KEY (`id`),
    KEY (`ip`),
    KEY (`gatetag`),
    KEY `date` (`date`),
    KEY `installed` (`installed`),
    KEY `md5` (`md5`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;";


    //$conn = mysql_connect('208.88.18.27','webuser','sunnytown123456');
    $conn = mysql_connect('172.18.128.65','frpgDeveloper','IUvCz0NLAuZp7r2iS1ic');
    mysql_select_db('analysis_st',$conn);
    
    $droptable = "truncate table {$after_table_name}";
    
    $rs = mysql_query($droptable,$conn);
    if (!$rs) 
    {
       echo "{$after_table_name} truncate table".mysql_error();
    } 
    else 
    {
        echo "{$after_table_name} drop Table  success.\n";
    }
     $rs = mysql_query($sql_daily_weblogs,$conn);
    if (!$rs) 
    {
        exit('Daily Table create failed'.mysql_error());
    } 
    else 
    {
        echo "{$after_table_name} Table create success.\n";
    }
    
    return $conn;
    
    
}
// 根据数据库的请求入口分析impseeview
function parselogbydbmd5($curday)
{
    $day = str_replace("-","",$curday);
    $dirpath = "/data/test/{$day}";
    $conn = mysql_connect('172.18.128.65','frpgDeveloper','IUvCz0NLAuZp7r2iS1ic');
    mysql_select_db('analysis_st',$conn);

    $sql = "select ip, md5 from fbpostgate_{$day} group by md5";
    
    $rs = mysql_query($sql, $conn);
    if (!$rs) 
    {
        exit('Daily Table create failed'.mysql_error());
    } 
    $eregid_str = '|([0-9\.]*) ([a-zA-Z0-9\.]*) - \[(.*)\] "[get\|post]* ([^"]*) HTTP/([0-9\.]*)" ([0-9]*) ([0-9]*) "([^"]*)" "([^"]*)" "([^"]*)"|i';
    $row = mysql_fetch_array($rs);
    $oldusercount = 0;
    $newusercount = 0;
    $onlyonepostcount = 0;
    $md5postcount = array();
    $FbDeauthorizecount = 0;
    $installedcount = 0;
    $fileinstall = fopen("/data/test/installfile_{$day}",'w');
    $filepostsize1000 = fopen("/data/test/filepostsize1000_{$day}",'w');
    $fileotherpoturlfile = fopen("/data/test/otherposturl_{$day}",'w');
    $installnomdfile = fopen("/data/test/installnomdfile_{$day}",'w');
    $impessview = 0;
    $acceptimpessviewcount = 0;
     $gaterequestsum = array();
    while($row)
    {
        $filehandle = fopen($dirpath.'/'.$row['ip'].'.log','r');
        if( $filehandle == null)
        {
            $row = mysql_fetch_array($rs);
            echo $dirpath.'/'.$row['ip'].'.log';
            continue;
        }
        $findonepost = false;
        $findpostcount = false;
        $findgetcount = false;
        $readindex = 0;
        $acceptpostgatearray = array();
       
        while (!feof($filehandle)) 
        {
            $strline = fgets($filehandle,'1024');
            $readindex = $readindex + 1;
            if (stristr($strline,'FbDeauthorize'))
            {
                $FbDeauthorizecount = $FbDeauthorizecount + 1;
            }
            if (strstr($strline,'POST') != false && stristr($strline,'installed=1') != false)
            {
                $installedcount = $installedcount + 1;
                fwrite($fileinstall,$row['ip']."\n",strlen($row['ip']."\n"));
            }
            if (strstr($strline,'POST') != false && stristr($strline,'apps.facebook.com/sunnytown') != false && stristr($strline,'installed=1') == false )
            {
                $findpostcount = true;
                
                preg_match_all($eregid_str, $strline, $matches, PREG_SET_ORDER);
                $postretsize = 0;
                $ip = '';
                $gatetag = '';
                $browseinfo = '';
                $gateurl = '';
                $findpostgate = false;
                $md5data = '';
                $postretsize = 0;
                foreach ($matches as $key => $val)
                {
                     if(stristr($val[4],'feed%2freward') == false 
                        && stristr($val[4],'feed/freward') == false 
                        && stristr($val[4],'gift%2freceive') == false
                        && stristr($val[4],'gift/receive') == false
                        && stristr($val[4],'gift%2freceivefromneigbhor') == false
                        && stristr($val[4],'gift/freceivefromneigbhor') == false
                        && stristr($val[4],'gift%2freceiveitem') == false
                        && stristr($val[4],'gift/receiveitem') == false
                        && stristr($val[4],'neighbor%2faccept') == false
                        && stristr($val[4],'neighbor/accept') == false
                        && stristr($val[4],'request%2facceptbuildinghelp') == false
                        && stristr($val[4],'request/acceptbuildinghelp') == false
                        && stristr($val[4],'request%2facceptrequiregift') == false
                        && stristr($val[4],'request/acceptrequiregift') == false
                        && stristr($val[4],'train%2ftrainaccept') == false
                        && stristr($val[4],'train/trainaccept') == false
                        && stristr($val[4],'bookmarks') == false
                        && stristr($val[4],'notif') == false
                        && stristr($val[4],'canvas_bkmk_more') == false
                        && stristr($val[4],'canvas_bkmk_top') == false
                        && stristr($val[4],'games_friends_apps') == false
                        && stristr($val[4],'games_my_recent') == false
                        && stristr($val[4],'discovery') == false
                        && stristr($val[4],'friends') == false
                        && stristr($val[4],'ts') == false
                        && stristr($val[4],'/?count') == false
                        && stristr($val[4],'appd_my_recent') == false
                        && ($val[4] =="/") == false
                        && stristr($val[4],'Farm%2FAcceptCropHelp') == false
                        && stristr($val[4],'Business%2FAcceptHelp') == false
                    )
                    {
                    //echo "readindex:".$readindex."\n";
                    //echo "fileotherpoturlfile:".$val[0]."\n";
                    fwrite($fileotherpoturlfile,$val[0]."\n",strlen($val[0]."\n"));
                    continue;
                    }
                    
                    
                    $date = date("Y-m-d H:i:s",strtotime($val[3]));
                    $ip = $val[1];
                    $gatetag = $val[4];
                    $browseinfo = $val[9];
                    $gateurl = $val[8];
                    $findpostgate = true;
                    $md5data = md5($ip.$browseinfo);
                    $postretsize = $val[7];
                    $poststatecode = $val[6];
                    if($md5data == $row['md5'])
                    {
                        //echo 'find same md5'.$md5data."\n";
                        $findpostgate = true;
                    }
                    
                
                }

                //echo "goto readindex:".$readindex.",findpostgate:".$findpostgate.",postretsize:".$postretsize."\n";
                if($findpostgate && $postretsize > 200 && $postretsize < 1000 && $poststatecode != 502)
                {
                    
                    $gaterequesttype = getrequesttype($val[4]);
                    
                    $requestconstant = getrequesttypenumber($gaterequesttype);
                    
                    if (!isset($gaterequestsum[$requestconstant]))
                        $gaterequestsum[$requestconstant] = 1;
                    else
                        $gaterequestsum[$requestconstant] = $gaterequestsum[$requestconstant] + 1;
                     
                    
                    if( stristr($val[4],'Farm%2FAcceptCropHelp') == true|| stristr($val[4],'Business%2FAcceptHelp') == true)
                        echo "{$ip}:".$val[4]." requestconstant:{$requestconstant} {$gaterequesttype}\n";
                    
                    $md5postcount[$row['md5']] = 1;
                    //echo "readindex:".$readindex."\n";
                    //echo "filepostsize1000:".$val[0]."\n";
                    //fwrite($filepostsize1000,$row['ip']."\n",strlen($row['ip']."\n"));
                    fwrite($filepostsize1000,$val[0]."\n",strlen($val[0]."\n"));
                    $impessview = $impessview + 1;
                    
                    $acceptpostgatearray[$md5data] = true;
                }
            }
            elseif (strstr($strline,'POST') != false && stristr($strline,'apps.facebook.com/sunnytown') != false && stristr($strline,'installed=1') != false)
            {
                preg_match_all($eregid_str, $strline, $matches, PREG_SET_ORDER);
                $postretsize = 0;
                $ip = '';
                $gatetag = '';
                $browseinfo = '';
                $gateurl = '';
                $findpostgate = false;
                $md5data = '';
                $postretsize = 0;
                foreach ($matches as $key => $val)
                {
                    /*  if(stristr($val[4],'feed%2freward') == false 
                        && stristr($val[4],'feed/freward') == false 
                        && stristr($val[4],'gift%2freceive') == false
                        && stristr($val[4],'gift/receive') == false
                        && stristr($val[4],'gift%2freceivefromneigbhor') == false
                        && stristr($val[4],'gift/freceivefromneigbhor') == false
                        && stristr($val[4],'gift%2freceiveitem') == false
                        && stristr($val[4],'gift/receiveitem') == false
                        && stristr($val[4],'neighbor%2faccept') == false
                        && stristr($val[4],'neighbor/accept') == false
                        && stristr($val[4],'request%2facceptbuildinghelp') == false
                        && stristr($val[4],'request/acceptbuildinghelp') == false
                        && stristr($val[4],'request%2facceptrequiregift') == false
                        && stristr($val[4],'request/acceptrequiregift') == false
                        && stristr($val[4],'train%2ftrainaccept') == false
                        && stristr($val[4],'train/trainaccept') == false
                        && stristr($val[4],'bookmarks') == false
                        && stristr($val[4],'notif') == false
                        && stristr($val[4],'canvas_bkmk_more') == false
                        && stristr($val[4],'canvas_bkmk_top') == false
                        && stristr($val[4],'games_friends_apps') == false
                        && stristr($val[4],'games_my_recent') == false
                        && stristr($val[4],'discovery') == false
                        && stristr($val[4],'friends') == false
                        && stristr($val[4],'ts') == false
                        && stristr($val[4],'/?count') == false
                        && ($val[4] =="/") == false
                    )
                    {
                        continue;
                    } */
                
                    $date = date("Y-m-d H:i:s",strtotime($val[3]));
                    $ip = $val[1];
                    $gatetag = $val[4];
                    $browseinfo = $val[9];
                    $gateurl = $val[8];
                    $findpostgate = true;
                    $md5data = md5($ip.$browseinfo);
                    $postretsize = $val[7];
                    if($md5data == $row['md5'])
                    {
                        //echo 'find same md5'.$md5data."\n";
                        if(isset($acceptpostgatearray[$md5data]) && $acceptpostgatearray[$md5data] == true)
                        {
                            $acceptimpessviewcount = $acceptimpessviewcount + 1;
                            $acceptpostgatearray[$md5data] = false;
                        }
                        else
                        {
                            fwrite($installnomdfile,$strline,strlen($strline));
                        }
                        
                        
                    }
                    else
                    {
                        fwrite($installnomdfile,$strline,strlen($strline));
                    }
                }

            }
            
           
        }
        
        $row = mysql_fetch_array($rs);
    }
    fclose($fileotherpoturlfile);
    fclose($filepostsize1000);
    fclose($fileinstall);
    fclose($installnomdfile);
    echo $curday.'_'.'oldusercount:'.$oldusercount."\n";
    echo $curday.'_'.'newusercount:'.$newusercount."\n";
    echo $curday.'_'.'md5postcount:'.count($md5postcount)."\n";
    echo $curday.'_'.'FbDeauthorizecount:'.$FbDeauthorizecount."\n";
    echo $curday.'_'.'$installedcount'.$installedcount."\n";
    echo $curday.'_'.'impessview'.$impessview."\n";
    echo $curday.'_'.'acceptimpessviewcount'.$acceptimpessviewcount."\n";
    
    

    $insertimpressviewsql = "insert into statistics_impression_pst (day,type,impression) values ";
    
    
    $valuesgaterequestsql = "";
    foreach($gaterequestsum as $k=>$v)
    {
        $valuesgaterequestsql = $valuesgaterequestsql." ('{$curday}','{$k}','{$v}'),";
        
    }
    $valuesgaterequestsql[strlen($valuesgaterequestsql) - 1] = ';';
    $insertimpressviewsql = $insertimpressviewsql.$valuesgaterequestsql;
    $insertimpressviewsql = $insertimpressviewsql."\n";
    $insertsqlgatefile = fopen("/data/test/insertsqlgate",'a');
    fwrite($insertsqlgatefile,$insertimpressviewsql,strlen($insertimpressviewsql));
    fclose($insertsqlgatefile);

    ////////////////////////////////////////////////////
    
    
    
    $dbuser = "webloguser";
    $dbpassword = "H29G0fs9JnRgOi2N322f";
    
    $curconn = mysql_connect("172.18.128.65",$dbuser,$dbpassword);
    mysql_select_db("frpg",$curconn);

    if(!$curconn)
    {
        echo mysql_error()."\n";
        exit();
    }
    $sql = $insertimpressviewsql;
        
    $rs = mysql_query($sql, $curconn);

    if(!$rs)
    {
        echo mysql_error()."\n";
        echo $sql."\n";
        exit();
    }
    echo "inser db ok\n";
    
    mysql_close($curconn);
    
    $logresult = fopen("/data/test/resultweblog",'a');
    $writedata = $curday." impessview:".$impessview.","."acceptimpessviewcount:".$acceptimpessviewcount."\n";
    fwrite($logresult,$writedata.strlen($writedata));
    fclose($logresult);
    
    
}


// 把前天的2个小时内写入当天
function parseiplogfileinhour($daydata)
{
    
    $day = date("Ymd",strtotime($daydata." 00:00:00") + 3600 * 24);
    if(PATH_SEPARATOR==':') 
        $dirpath = "/data/test/{$day}";
    else 
        $dirpath = 'E:\\20110731';
    
    
    if(!is_dir($dirpath))
        parselog(date("Y-m-d",strtotime($daydata." 00:00:00") + 3600 * 24));
   
    $aDirectory = new DirReader($dirpath);
    $fileListArray = $aDirectory->getFileList();
    
    $eregid_str = '|([0-9\.]*) ([a-zA-Z0-9\.]*) - \[(.*)\] "[get\|post]* ([^"]*) HTTP/([0-9\.]*)" ([0-9]*) ([0-9]*) "([^"]*)" "([^"]*)" "([^"]*)"|i';
    $aff_data = array();
    $i = 0;

    $log_file    = "/data/test/access_{$day}.log";
    if(!file_exists($log_file))
    {
        $fileh = fopen($log_file,'a');
        fclose($fileh);
        
        $sh = "zcat /data/weblogs/st.ucool.com.access.log.{$day}.gz > {$log_file}";
        exec($sh);
    }
    
    
    $ff = fopen($log_file,'r');
    if($ff == null)
    {
        echo $log_file. ' is Not File';
        return;
    }
    
    $run_time = 0;
    $ipmappostorget = array();
    $str = '';
    $dateend = strtotime(date("Y-m-d",strtotime($daydata." 00:00:00") + 3600 * 24)." 02:00:00");
    $writeday = str_replace("-","",$daydata);;
    // -- get data from full web logs
   
    $writelogdir = "/data/test/{$writeday}";
    
    while (!feof($ff)) 
    {
        $strline = fgets($ff,'1024');
        
        preg_match_all($eregid_str, $strline, $matches, PREG_SET_ORDER);

        foreach ($matches as $key => $val) 
        {
            if (11 == count($val))
            {
                if(strtotime($val[3]) < $dateend)
                {
                    $iptofile = $writelogdir."/{$val[1]}.log";
                    
                    if (file_exists($iptofile))
                    {
                        $findsame = false;
                        $iplogfile = fopen($iptofile,"r");
                        
                        while (!feof($iplogfile)) 
                        {
                            $tmpstrline = fgets($iplogfile,'1024');
                            
                            preg_match_all($eregid_str, $tmpstrline, $temmatches, PREG_SET_ORDER);
                             
                            foreach ($temmatches as $keyttt => $valttt) 
                            {
                                if (11 == count($valttt))
                                {
                                    if (stristr($val[3],$valttt[3]) == true 
                                        && stristr($val[1],$valttt[1]) == true
                                        && stristr($val[8],$valttt[8]) == true
                                        && stristr($val[9],$valttt[9]) == true
                                        && stristr($val[4],$valttt[4]) == true
                                        )
                                    {
                                        //echo $valttt[3]." writed {$valttt[1]}.log file \n";
                                        $findsame = true;
                                    }
                                }
                            }
                            if ($findsame)
                                break;
                            
                            
                        }
                        
                        fclose($iplogfile);
                        
                        if ($findsame == false)
                        {
                            $iplogfile = fopen($iptofile,"a");
                            $logdata = $val[0]."\n";
                            fwrite($iplogfile,$logdata,strlen($logdata));
                            fclose($iplogfile);
                        }
                        
                        
                    }
                    else
                    {
                        $iplogfile = fopen($iptofile,"a");
                        $logdata = $val[0]."\n";
                        fwrite($iplogfile,$logdata,strlen($logdata));
                        fclose($iplogfile);
                    }
                    
                    //echo "write:".$writelogdir."/".$val[1].".log:".$val[3]."\n";
                }
                
            }
        }
    }
    fclose($ff);
    
}

// 
function getfirstnewplayerpostgate()
{
    //parselog();
    
    $day = str_replace("-","",DAY);
    if(PATH_SEPARATOR==':') 
        $dirpath = "/data/test/{$day}";
    else 
        $dirpath = 'E:\\20110731';
    
    $aDirectory = new DirReader($dirpath);
    $fileListArray = $aDirectory->getFileList();
    
    $eregid_str = '|([0-9\.]*) ([a-zA-Z0-9\.]*) - \[(.*)\] "[get\|post]* ([^"]*) HTTP/([0-9\.]*)" ([0-9]*) ([0-9]*) "([^"]*)" "([^"]*)" "([^"]*)"|i';
    $posturl = array();
    foreach($fileListArray as $filename)
    {
        
        $filehandle = fopen($dirpath.'/'.$filename,'r');
        
        while (!feof($filehandle)) 
        {
            $strline = fgets($filehandle,'1024');
            
            if (strstr($strline,'POST') != false && stristr($strline,'apps.facebook.com/sunnytown') != false)
            {
                preg_match_all($eregid_str, $strline, $matches, PREG_SET_ORDER);

                foreach ($matches as $key => $val) 
                {
                    if(strpos($val[4],"&") == true)
                    {
                       
                        $keydata = substr($val[4],0,strpos($val[4],"&"));
                        $posturl[$keydata] = 1;       
                    }
                    else
                    {
                       
                        $posturl[$val[4]] = 1;       
                    }
                    
                }
            }
            break;
        }
        fclose($filehandle);
    }
    $postgateurlfile = fopen('/data/test/postgateurl'.$day,'w');
    foreach($posturl as $k=>$v)
    {
        fwrite($postgateurlfile,$k."\n",strlen($k."\n"));
        
    }
    fclose($postgateurlfile);
    
}
function filter($str)
{
    if(stristr($str,'feed%2freward') == false 
                        && stristr($str,'feed/freward') == false 
                        && stristr($str,'gift%2freceive') == false
                        && stristr($str,'gift/receive') == false
                        && stristr($str,'gift%2freceivefromneigbhor') == false
                        && stristr($str,'gift/freceivefromneigbhor') == false
                        && stristr($str,'gift%2freceiveitem') == false
                        && stristr($str,'gift/receiveitem') == false
                        && stristr($str,'neighbor%2faccept') == false
                        && stristr($str,'neighbor/accept') == false
                        && stristr($str,'request%2facceptbuildinghelp') == false
                        && stristr($str,'request/acceptbuildinghelp') == false
                        && stristr($str,'request%2facceptrequiregift') == false
                        && stristr($str,'request/acceptrequiregift') == false
                        && stristr($str,'train%2ftrainaccept') == false
                        && stristr($str,'train/trainaccept') == false
                        && stristr($str,'bookmarks') == false
                        && stristr($str,'notif') == false
                        && stristr($str,'canvas_bkmk_more') == false
                        && stristr($str,'canvas_bkmk_top') == false
                        && stristr($str,'games_friends_apps') == false
                        && stristr($str,'games_my_recent') == false
                        && stristr($str,'discovery') == false
                        && stristr($str,'friends') == false
                        && stristr($str,'ts') == false
                        && stristr($str,'/?count') == false
                        && stristr($str,'appd_my_recent') == false
                        && ($str =="/") == false
        )
        return false;
    
    return true;
                    
}
function getrequesttype($str)
{
    
    $retgatetag = array('feed%2freward', 
                        'feed/freward', 
                        'gift%2freceive',
                        'gift/receive',
                        'gift%2freceivefromneigbhor',
                        'gift/freceivefromneigbhor',
                        'gift%2freceiveitem',
                        'gift/receiveitem',
                        'neighbor%2faccept',
                        'neighbor/accept',
                        'request%2facceptbuildinghelp',
                        'request/acceptbuildinghelp',
                        'request%2facceptrequiregift',
                        'request/acceptrequiregift',
                        'train%2ftrainaccept',
                        'train/trainaccept',
                        'bookmarks',
                        'notif',
                        'canvas_bkmk_more',
                        'canvas_bkmk_top',
                        'games_friends_apps',
                        'games_my_recent',
                        'discovery',
                        'friends',
                        'ts',
                        '/?count',
                        'appd_my_recent',
                        "Farm%2FAcceptCropHelp",
                        "Business%2FAcceptHelp",
                        "/"
                        );
    foreach($retgatetag as $filter)
    {
        if (stristr($str,$filter) == true)
            return $filter;
    }
    return 'nil';
}
function getrequesttypenumber($str)
{
    if(stristr($str,'feed%2f') == true)
        return 6;
    if(stristr($str,'gift%2f') == true)
        return 1;
    if(stristr($str,'neighbor%2f') == true)
        return 2;
    if(stristr($str,'request%2f') == true)
    {
        if(stristr($str,'request%2facceptbuildinghelp') == true)
            return 4;
        return 3;
    }
        
    
    if(stristr($str,'train') == true)
        return 5;
    if(stristr($str,'discovery') == true)
        return 8;
    if(stristr($str,'bookmarks') == true)
        return 71;
    if(stristr($str,'canvas_bkmk_more') == true)
        return 72;
    if(stristr($str,'canvas_bkmk_top') == true)
        return 73;
    if(stristr($str,'games_friends_apps') == true)
        return 74;
    if(stristr($str,'games_my_recent') == true)
        return 75;
    if(stristr($str,'appd_my_recent') == true)
        return 76;
    if(stristr($str,'notif') == true)
        return 77;
    if(stristr($str,'ts') == true)
        return 78;
    if(stristr($str,'Direct') == true)
        return 0;
    if(stristr($str,'Farm%2FAcceptCropHelp') == true)
        return 11;
    if(stristr($str,'Business%2FAcceptHelp') == true)
        return 10;
    
 /*    $arr_request_type = array
    (
    0 => 'Direct',
    1 => 'Gift',
    2 => 'Neighbor',
    3 => 'Ask Gift',
    4 => 'Position',
    5 => 'Helicopter',
    6 => 'Feed',
    7 => 'ref',
    8 => 'type',
    9 => 'AD',
    
    71 => 'bookmarks',
    72 => 'canvas_bkmk_more',
    73 => 'canvas_bkmk_top',
    74 => 'games_friends_apps',
    75 => 'games_my_recent',
    76 => 'appd_my_recent',
    77 => 'notif',
    78 => 'ts',
    ); */

   
    return 0;

}

function getweblogfromdb($day,$ip)
{
    $conn = mysql_connect('64.156.194.41','st_web_report','k1ftCm78Ln4CtiAffhhR');
    mysql_select_db('st_analyse_web_log',$conn);
    
    $hours = array("00","01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23");
    $arrayrequest = array();
    foreach($hours as $hour)
    {
        $access_datetable = "access_{$day}".$hour;
    
        $sql = "select parameter from {$access_datetable} where ip='{$ip}';";
        $rs = mysql_query($sql,$conn);
        if (!$rs) 
        {
            //echo "{$access_datetable} no exsits\n";
            //echo $sql."\n";
            continue;
        } 
        
        if( mysql_num_rows($rs) == 0)
            continue;
        
        $ar = mysql_fetch_array($rs);
            
        $questList = array();
            
        while($ar)
        {
            $arrayrequest[] = $ar['parameter'];
            $ar = mysql_fetch_array($rs);
        }
        
    }
    
    return $arrayrequest;
    
}
function parasebugreportfromdb()
{
    

    $allbuglogfile = fopen("all.log",'r');
    
    if($allbuglogfile == null)
    {
        echo "open file fail";
        return;
    }
    
    $arrayloginstate = array(
    "WEB_SHOW_FLASH",
    "LOADING_SWF_OK",
    "CLIENT_LOAD_OK",
    "CONNECT_SUCCESS",
    "CONNECT_FAILED",
    "SendDoLogin",
    "onReceive",
    "LOGIN_OK",
    "PLAYER_OBJECT_LOAD",
    "LOAD_FARM",
    "LOAD_ASSETS_OK",
    "CONNECT_STARTED",
    "SCENE_INIT_OK",
    "START_GAME");
    $eregid_str = '/\|/';
    
   
    
    $eregid_str2 = '|([0-9\.]*) ([a-zA-Z0-9\.]*) - \[(.*)\] "[get\|post]* ([^"]*) HTTP/([0-9\.]*)" ([0-9]*) ([0-9]*) "([^"]*)" "([^"]*)" "([^"]*)"|i';
    
    $countreport = 0;
    $bugwebloginstate = array();
    $uidloginstate = array();
    $allweblogfile = fopen("allappendweblog",'w');
    $weblogheader="uid|ip|report_time|start_at|end_at|reg_time|user_last_login_time|fbuid|email|player_last_login_time|connected|start_connect|server_address|reconnect|onreived|client_loaded|client_version|SecurityError|connect_timeout|login_timeout|assets_loaded_num|assets_complete|onQuestListResult|version|man|resolution|OS|language|PT|file|";
    foreach($arrayloginstate as $filterstr)
    {
         $weblogheader = $weblogheader.$filterstr."|";
    }
    $weblogheader = $weblogheader."reason|";
    $weblogheader = $weblogheader."\n";
    fwrite($allweblogfile,$weblogheader,strlen($weblogheader));
    while (!feof($allbuglogfile))
    {
        $str = fgets($allbuglogfile,'1024');
        
        $matches = preg_split($eregid_str, $str);
         
        if($matches != null && $matches[0] != "uid" && count($matches) > 20)
        {
            $bugwebloginstate[$countreport] = array();
            
            $countreport = $countreport + 1;
            
           	$uid = $matches[0];
           	$ip = $matches[1];
           	$report_time = $matches[2];
           	$start_at = $matches[3];
           	$end_at = $matches[4];
           	$reg_time = $matches[5];
           	$user_last_login_time = $matches[6];
           	$fbuid = $matches[7];
            $email = $matches[8];
           	$player_last_login_time = $matches[9];
           	$connected = $matches[10];
           	$start_connect = $matches[11];
           	$server_address = $matches[12];
           	$reconnect = $matches[13];
            $onreived = $matches[14];
           	$client_loaded = $matches[15];
           	$client_version = $matches[16];
           	$SecurityError = $matches[17];
           	$connect_timeout = $matches[18];
           	$login_timeout = $matches[19];
           	$assets_loaded_num = $matches[20];
           	$assets_complete = $matches[21];
            $onQuestListResult = $matches[22];
           	$version = $matches[23];
           	$man = $matches[24];
           	$resolution = $matches[25];
           	$OS = $matches[26];
           	$language = $matches[27];
            if(isset($matches[28]))
                $PT = $matches[28];
            if(isset($matches[29]))
                $file = $matches[29];
            
            $day = substr($report_time,0,8);
            $dbweblogs = getweblogfromdb($day,$ip);
            $lastloginstate = '';
            
            if($uid == null || !isset($uid))
            {
                echo "uid is null !!!\n".$str."\n";
                continue;
            }
                
            if(count($dbweblogs) == 0)
            {
                echo "{$day}-{$ip} no find weblogs !!!\n".$str."\n";
                continue;
            }
            foreach($dbweblogs as $weblogstr)
            {
                //echo $weblogstr."\n";
                
                if(stristr($weblogstr,$uid) != false)
                {
                    if(!isset($uidloginstate[$uid]))
                        $uidloginstate[$uid] = array();

                    foreach($arrayloginstate as $loginstate)
                    {
                               
                        if(stristr($weblogstr,$loginstate) != false)
                        {
                            if (!isset($uidloginstate[$uid][$loginstate]))
                                $uidloginstate[$uid][$loginstate] = 1;
                            else
                                $uidloginstate[$uid][$loginstate] = $uidloginstate[$uid][$loginstate] + 1;
                        }
                                       
                    }

                }        
                
            }
            $str = str_replace("\n","",$str);
            foreach($arrayloginstate as $filterstr)
            {
                if(isset($uidloginstate[$uid][$filterstr]))
                    $str = $str.$uidloginstate[$uid][$filterstr]."|";
                else
                    $str = $str."0|";
                
            }
            
            if(($assets_complete == 0 || !isset($uidloginstate[$uid]['LOAD_ASSETS_OK']) || $uidloginstate[$uid]['LOAD_ASSETS_OK'] == 0)
                && !isset($uidloginstate[$uid]['START_GAME']))
                $str = $str."ASSETS NO COMPLETE|";
            elseif (isset($uidloginstate[$uid]['START_GAME']) && $uidloginstate[$uid]['START_GAME'] > 0)
                $str = $str."START GAME OK|";
            elseif ((isset($uidloginstate[$uid]['SendDoLogin']) && $uidloginstate[$uid]['SendDoLogin'] > 0) && 
                ($onreived == 0 
                    ||!isset($uidloginstate[$uid]['onReceive'])
                    ||(isset($uidloginstate[$uid]['onReceive']) && $uidloginstate[$uid]['onReceive'] == 0)))
                $str = $str."SendDoLogin But no Receive|";
            elseif ($onQuestListResult > 0)
                $str = $str."START GAME OK|";
            elseif ($client_loaded == 0)
                $str = $str."client no loaded|";
            else
                $str = $str."I am don't know'|";
            $str = $str."\n";
            
            
            echo $str;
            fwrite($allweblogfile,$str,strlen($str));
            
            
        }
        else
        {
            echo "no parase:".$str."\n";
        }
        
    }
    fclose($allweblogfile);
    fclose($allbuglogfile);
    
    
}
// 分析dany 生成的all.log文件 查找web logs
function parasebugreport()
{
    
     if(PATH_SEPARATOR==':') 
        $dirpath = "/data/test/";
    else 
        $dirpath = 'E:\\';
    
    $allbuglogfile = fopen($dirpath."all.log",'r');
    
    if($allbuglogfile == null)
    {
        echo "open file fail";
        return;
    }
    
    
    
    $arrayloginstate = array(
    "WEB_SHOW_FLASH",
    "LOADING_SWF_OK",
    "CLIENT_LOAD_OK",
    "CONNECT_SUCCESS",
    "SendDoLogin",
    "onReceive",
    "LOGIN_OK",
    "PLAYER_OBJECT_LOAD",
    "LOAD_FARM",
    "LOAD_ASSETS_OK",
    "CONNECT_STARTED",
    "SCENE_INIT_OK",
    "START_GAME");
    $eregid_str = '/\|/';
    
   
    
    $eregid_str2 = '|([0-9\.]*) ([a-zA-Z0-9\.]*) - \[(.*)\] "[get\|post]* ([^"]*) HTTP/([0-9\.]*)" ([0-9]*) ([0-9]*) "([^"]*)" "([^"]*)" "([^"]*)"|i';
    
    $countreport = 0;
    $bugwebloginstate = array();
    $allweblogfile = fopen("allappendweblog",'w');
    $weblogheader="uid|ip|report_time|start_at|end_at|reg_time|user_last_login_time|fbuid|player_last_login_time|connected|start_connect|server_address|reconnect|client_loaded|client_version|SecurityError|connect_timeout|login_timeout|assets_loaded_num|assets_complete|version|man|resolution|OS|language|PT|file|lastloginstate|lastloadassit|\n";
    fwrite($allweblogfile,$weblogheader,strlen($weblogheader));
    while (!feof($allbuglogfile))
    {
        $str = fgets($allbuglogfile,'1024');
        
        $matches = preg_split($eregid_str, $str);
         
        if($matches != null && $matches[0] != "uid" && count($matches) > 20)
        {
            $bugwebloginstate[$countreport] = array();
            
            $countreport = $countreport + 1;
           	$uid = $matches[0];
           	$ip = $matches[1];
           	$report_time = $matches[2];
           	$start_at = $matches[3];
           	$end_at = $matches[4];
           	$reg_time = $matches[5];
           	$user_last_login_time = $matches[6];
           	$fbuid = $matches[7];
           	$player_last_login_time = $matches[8];
           	$connected = $matches[9];
           	$start_connect = $matches[10];
           	$server_address = $matches[11];
           	$reconnect = $matches[12];
           	$client_loaded = $matches[13];
           	$client_version = $matches[14];
           	$SecurityError = $matches[15];
           	$connect_timeout = $matches[16];
           	$login_timeout = $matches[17];
           	$assets_loaded_num = $matches[18];
           	$assets_complete = $matches[19];
           	$version = $matches[20];
           	$man = $matches[21];
           	$resolution = $matches[22];
           	$OS = $matches[23];
           	$language = $matches[24];
           	$PT = $matches[25];
           	$file = $matches[26];
            
            
            //$logdiripfilepath = $dirpath.date("Ymd",strtotime($user_last_login_time))."/{$ip}.log";
            $logdiripfilepath = $dirpath.substr($report_time,0,8)."/{$ip}.log";
            if (!file_exists($logdiripfilepath))
            {
                echo $logdiripfilepath." no exist.{$report_time}\n";
                continue;
            }
            $filelog = fopen($logdiripfilepath,'r');
            
            if($filelog != null)
            {
                $lastloginstate = "";
                $lastloginstate2 = "";
                $sendlogin = "";
                $recv = "";
                while (!feof($filelog))
                {
                    $weblogstr = fgets($filelog,'1024');
                    
                    if(stristr($weblogstr,$uid) != false)
                    {
                        if(!isset($uidloginstate[$uid]))
                            $uidloginstate[$uid] = array();
                        
                            
                        
                        foreach($arrayloginstate as $loginstate)
                        {
                           
                            if(stristr($weblogstr,$loginstate) != false)
                            {
                                if($loginstate == "LOADING_SWF_OK" 
                                    || $loginstate == "CLIENT_LOAD_OK"
                                    || $loginstate == "LOAD_ASSETS_OK")
                                    $lastloginstate2 = $loginstate;
                                else
                                    $lastloginstate = $loginstate;
                                     
                                
                            
                                if (!isset($uidloginstate[$uid][$loginstate]))
                                    $uidloginstate[$uid][$loginstate] = 1;
                                else
                                    $uidloginstate[$uid][$loginstate] = $uidloginstate[$uid][$loginstate] + 1;
                            }
                            
                            
                           
                           
                        }

                    }    
                    
                }
                fclose($filelog);
                
                
                $str = str_replace("\n","",$str)."|".$lastloginstate."|".$lastloginstate2."\n";
               
                fwrite($allweblogfile,$str,strlen($str));
                
                
            }
            else
            {
                echo "fopen ".$logdiripfilepath." fail";
            }
            

            
        }
        else
        {
            echo "no parase:".$str."\n";
        }
        
    }
    fclose($allweblogfile);
    fclose($allbuglogfile);
    
    $uidloglogininfo = fopen($dirpath."/allloginstaewitweblog",'w');
    foreach($uidloginstate as $k => $v)
    {
        
        $writeloginstatinfo = "uid={$k},";
        foreach($v as $kk => $vv)
        {
            $writeloginstatinfo = $writeloginstatinfo."{$kk}={$vv},";
        }
        $writeloginstatinfo = $writeloginstatinfo."\n";
        fwrite($uidloglogininfo,$writeloginstatinfo,strlen($writeloginstatinfo));
        
    }
    $START_GAMEcount = 0;
    $Allreportcount = count($uidloginstate);
    foreach($uidloginstate as $k => $v)
    {
        foreach($v as $kk => $vv)
        {
            if($kk == "START_GAME")
            {
                $START_GAMEcount = $START_GAMEcount + 1;
                break;
            }
        }
       
    }
    $writeloginstatinfo = "START_GAME:{$START_GAMEcount},allreport:{$Allreportcount},countreport:{$countreport}\n";
    fwrite($uidloglogininfo,$writeloginstatinfo,strlen($writeloginstatinfo));
    fclose($uidloglogininfo);
    
}

sumbotaccountupdatefile();

//getfirstnewplayerpostgate();
 
